package com.katze.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

public class MybatisPlusConfiguration {

    private static boolean mapUnderscoreToCamelCase;

    /**
     * 新的分页插件
     * 一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());//防全表更新与删除
        return interceptor;
    }


    @Value("${mybatis-plus.configuration.map-underscore-to-camel-case:true}")
    public void setMapUnderscoreToCamelCase(boolean mapUnderscoreToCamelCase){
        MybatisPlusConfiguration.mapUnderscoreToCamelCase = mapUnderscoreToCamelCase;
    }

    /**
     * 表字段名是否转驼峰
     * true: 说明表字段带有下划线
     * false: 说明表字段是驼峰
     */
    public static boolean isMapUnderscoreToCamelCase(){
        return mapUnderscoreToCamelCase;
    }
}
